/**
 * 
 */
package filePackets;

import java.io.*;

/**
 * This class represents the packets sent between the FileClient and the FileServer
 * these packets are sent through the UDP sockets.
 * a FilePacket is serializable in order to be transoformed to a byte array and sent as  
 * DatagramPacket.buf
 * 
 * any new packet type must be added to the PacketType enum and the new packet class must 
 * be inherited from FilePacket and implement a getPacketType() that returns the appropriate
 * PacketType
 * 
 * @author peleg
 */
public abstract class FilePacket implements Serializable{
	public enum PacketType{
		NEW_SESSION, DATA, OTHER;
	}; 
	private static final long serialVersionUID = -3010016379746592419L;
	public abstract PacketType getPacketType();
	public abstract void get_fields_from_obj();
	
	protected FilePacket obj;
	
	/**
	 * uses the Serializability of the class to transform it into a byte array
	 * @return byte[] - a byte array that is the message ready to be sent
	 * this method is implemented by each inheriting class 
	 * @throws IOException 
	 */
	abstract public byte[] toByteArray() throws IOException;
	
	FilePacket(byte[] ba) throws IOException, ClassNotFoundException{
		ByteArrayInputStream bis = new ByteArrayInputStream(ba);
		ObjectInputStream ois = new ObjectInputStream(bis);
		obj = (FilePacket)ois.readObject();
		bis.close();
		ois.close();
		get_fields_from_obj();
		obj = null;
	}

	public FilePacket() {
		super();
	}
}
